<!--财务报销制度-->
<template>
  <view>
    <van-sticky>
      <van-search
        v-model="title"
        placeholder="请输入搜索关键词"
        input-align="center"
        @input="onSearch"
      />
    </van-sticky>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="load_more"
                offset="20" :error.sync="error" error-text="请求失败，点击重新加载" :immediate-check="immediateCheck">
        <van-cell v-for="item in list" center :title="item.title" :label="'文号：'+item.extend2" :value="timeFormat(item.pushTime)"
                  value-class="text-sm" @click="openDetail(item.id)">
        </van-cell>
      </van-list>
      <van-dialog v-model="showPopup" title="相关附件" show-cancel-button :show-confirm-button=false>
        <van-cell v-for="item in annexsList" :title="item.annexName" title-class="text-blue" @click="openFile(item)"/>
      </van-dialog>
    </van-pull-refresh>
  </view>
</template>

<script>
import moment from 'moment';
export default {
  data() {
    return {
      list: [],
      loading: false,   // 加载状态
      finished: false,  // 是否加载
      refreshing: false,// 是否刷新
      error: false,     // 错误状态
      pageNum: 1,   		// 页数
      pageSize: 10, 		// 条数
      immediateCheck: true, // 是否在初始化时立即执行滚动位置检查　　　
      title:'',//搜索关键字
      searchContent:'',
      showPopup:false,   //如果有多个文件弹出窗口展示
      annexsList:[],     //多个文件
    }
  },
  methods: {
    timeFormat (value)
    {
      return moment(value).format('YYYY-MM-DD HH:mm');
    },
    onSearch(){
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        this.searchContent= this.title
        this.onRefresh()
      },500)

    },
    // 下拉刷新
    onRefresh() {
      // 先清空数据
      this.list = []
      // 将没有更多的状态改成false
      this.finished = false
      // 默认加载第一页
      this.pageNum = 1
      this.getList()
      this.refreshing = false
    },

    // 上拉加载
    load_more() {
      // 页数设置
      if (this.list.length == 0) {
        this.pageNum = 1
      } else {
        this.pageNum++
      }
      // 将下拉刷新停止
      this.refreshing = false;
      setTimeout(() => {
        this.getList()
      }, 500)
    },
    // 财务报销制度列表
    getList() {
      // 加载状态
      this.loading = true
      this.$http({
        url: this.$http.adornUrl('/caac/oa/app/getCwbxzd'),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'page': this.pageNum,
          'rows': this.pageSize,
          'title': this.searchContent
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.loading = false;
          if (this.util.isNotEmpty(data.data.data)) {
            if (this.pageNum == 1) {
              this.list = data.data.data.rows
            } else {
              data.data.data.rows.forEach(t => this.list.push(t))
            }
            // 判断当前页是否为最后一页
            if ((this.pageNum * this.pageSize) > data.data.data.total) {
              this.finished = true
            }
          } else {
            this.finished = true
          }
        } else {
          this.error = true
        }
      }).catch((err) => {
        this.error = true
      }).finally(() => {
        // 关闭加载状态
        this.loading = false
      })
    },
    //获取财务报销制度详情
    openDetail(id) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/app/getCwbxzdFormInfo`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,
          'systemFlag': this.cache.get('userInfo').systemFlag,
          'id': id,
        })
      }).then(({data}) => {
        if (data.code == 200) {
          console.log("data.data: " + JSON.stringify(data.data));
          const detaInfo = data.data.data
          if (this.util.isNotEmpty(detaInfo) && this.util.isNotEmpty(detaInfo.annexs)) {
            if (detaInfo.annexs.length > 1) {
              this.showPopup = true
              this.annexsList = detaInfo.annexs
            }else{
              this.openFile(detaInfo.annexs[0])

            }
          }
        }else{
          this.$toast('失败:'+data.message)
        }
      })
    },
    /**
     * 打开财务报销制度文件
     * @param annexInfo
     */
    openFile(item) {
      let attachId = item.attachId
      let attachName = this.util.trim(item.annexName).replaceAll(" ","")
      let attachType = attachName.substr(attachName.lastIndexOf('.')+1)
      if(attachId) {
        attachType = attachType.toLowerCase()
        let url = '/caac/oa/app/download?attachId=' + attachId +
                '&attachName='+attachName+
                '&systemKey=' + this.cache.get('userInfo').systemKey +
                '&systemFlag=' + this.cache.get('userInfo').systemFlag
        let openUrl = this.$http.adornUrl(url)
        // 打开文电
        this.lx.openFileDetail(attachName, attachType.toLowerCase(), openUrl)
      }else{
        this.$toast("当前文件不存在！")
      }
    }
  }
}
</script>

<style>

</style>
